<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>apache 配置静态缓存 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>apache 配置静态缓存</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的静态文件指的是图片、js、css等文件，用户访问一个站点，其实大多数元素都是图片、js、css等，这些静态文件其实是会被客户端的浏览器缓存到本地电脑上的，目的就是为了下次再请求时不再去服务器上下载，这样就加快了速度，提高了用户体验。但这些静态文件总不能一直缓存，它总有一些时效性。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line">&lt;IfModule mod_expires.c&gt;</div><div class="line">ExpiresActive on</div><div class="line">ExpiresByType image/gif <span class="string">"access plus 1 days"</span></div><div class="line">ExpiresByType image/jpeg <span class="string">"access plus 24 hours"</span></div><div class="line">ExpiresByType image/png <span class="string">"access plus 24 hours"</span></div><div class="line">ExpiresByType text/css <span class="string">"now plus 2 hour"</span></div><div class="line">ExpiresByType application/x-javascript <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresByType application/javascript <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresByType application/x-shockwave-flash <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresDefault <span class="string">"now plus 0 min"</span></div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试配置是否成功，并重启apache 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl graceful</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用curl 测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># curl -x127.0.0.1:80 'http://www.test.com/static/image/common/logo.png' -IHTTP/1.1 200 OK</span></div><div class="line">Date: Thu, 05 Jan 2017 06:33:18 GMT</div><div class="line">Server: Apache/2.2.31 (Unix) PHP/5.6.6</div><div class="line">Last-Modified: Tue, 31 May 2016 03:08:36 GMT</div><div class="line">ETag: <span class="string">"2854b-1149-5341ab0597500"</span></div><div class="line">Accept-Ranges: bytes</div><div class="line">Content-Length: 4425</div><div class="line">Cache-Control: max-age=86400</div><div class="line">Expires: Fri, 06 Jan 2017 06:33:18 GMT</div><div class="line">Content-Type: image/png</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者使用mod_headers 模块实现</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_headers.c&gt;</div><div class="line"><span class="comment">#htm,html,txt 类的文件缓存一小时</span></div><div class="line">&lt;filesmatch <span class="string">"\.(html|htm|txt)$"</span>&gt;</div><div class="line">header <span class="built_in">set</span> cache-control <span class="string">"max-age=3600"</span></div><div class="line">&lt;/filesmatch&gt;</div><div class="line"><span class="comment">#css,js,swf 类的文件缓存一个星期</span></div><div class="line">&lt;filesmatch <span class="string">"\.(cssl|js|swf)$"</span>&gt;</div><div class="line">header <span class="built_in">set</span> cache-control <span class="string">"max-age=604800"</span></div><div class="line">&lt;/filesmatch&gt;</div><div class="line"><span class="comment">#jpg,gif,jpeg,png,ico,flv,pdf 类的文件缓存一年</span></div><div class="line">&lt;filesmatch <span class="string">"\.(jpg|gif|jpeg|png|ico|flv|pdf)$"</span>&gt;</div><div class="line">header <span class="built_in">set</span> cache-control <span class="string">"max-age=29030400"</span></div><div class="line">&lt;/filesmatch&gt;</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：这里的时间单位可以是days，hours，甚至是min，两种不同的方法，要想使用这些模块，必须要事先已经支持。查看是否支持命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/apache2/bin/apachectl -M</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/37. apache 配置静态缓存/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
